package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmAlarmUpdateDto;
import com.kaili.basic.pojo.dto.MdmAlarmInfoDto;
import com.kaili.basic.pojo.dto.MdmAlarmQueryByPageDto;
import com.kaili.basic.pojo.dto.MdmAlarmQueryDto;
import com.kaili.basic.pojo.entity.MdmAlarmEntity;
import com.kaili.basic.pojo.vo.MdmAlarmDeviceVo;
import com.kaili.basic.pojo.vo.MdmAlarmQueryAllVo;
import com.kaili.basic.service.MdmAlarmService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Desc: MdmAlarmController
 * @author: jianfeng
 * @Time: 2019/7/24 16:30
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "告警管理")
public class MdmAlarmController {

    @Autowired
    private MdmAlarmService alarmService;

    @PutMapping("/alarm")
    @ApiOperation(value = "设置告警规则",notes = "设置告警规则")
    public RestStatus updateMdmAlarm(@RequestBody MdmAlarmUpdateDto alarmUpdateDto){
        alarmUpdateDto.setModifyBy(LoginDetailUtils.getCurrent().getId());
        alarmUpdateDto.setModifyTime(new Date());
        MdmAlarmEntity alarmEntity= ObjectUtils.toObject(alarmUpdateDto,MdmAlarmEntity.class);
        alarmService.updateMdmAlarm(alarmEntity);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/alarm")
    @ApiOperation(value = "查询告警设置信息")
    public List<MdmAlarmQueryAllVo> queryMdmAlarm(MdmAlarmQueryDto alarmQueryDto){
        return alarmService.queryMdmAlarm(alarmQueryDto);
    }

    @GetMapping("/alarm/pageList")
    @ApiOperation(value = "分页查询告警信息")
    public Page<MdmAlarmQueryAllVo> queryPageList(MdmAlarmQueryByPageDto alarmQueryByPageDto){
        return alarmService.queryPageList(new Page<>(alarmQueryByPageDto.getPage(),alarmQueryByPageDto.getPageSize()),alarmQueryByPageDto);
    }

    @GetMapping("/alarm/{id}")
    @ApiOperation(value = "根据id查询告警名称")
    public List<MdmAlarmQueryAllVo> queryMdmAlarmById(@PathVariable Long id){
        return alarmService.queryMdmAlarmById(id);
    }

    @GetMapping("/alarm/pageAlarm")
    @ApiOperation(value = "分页查询告警详情")
    public Page<MdmAlarmDeviceVo> queryPageListAlarm(MdmAlarmInfoDto alarmInfoDto){
        return alarmService.queryPageListAlarm(new Page<>(alarmInfoDto.getPage(),alarmInfoDto.getPageSize()),alarmInfoDto);
    }

    @GetMapping("alarm/export")
    @ApiOperation(value = "告警信息导出")
    public void export(HttpServletResponse response,@ModelAttribute MdmAlarmQueryByPageDto alarmQueryByPageDto) throws RestException {
        List<MdmAlarmQueryAllVo> list=alarmService.queryAlarmExport(alarmQueryByPageDto);
        ExcelUtil.exportExcel(list,"告警信息","sheet1",MdmAlarmQueryAllVo.class, UUID.randomUUID().toString()+".xls",response);
    }

    @GetMapping("/alarm/exportInfo")
    @ApiOperation(value = "告警详情导出")
    public void exportInfo(HttpServletResponse response,@ModelAttribute MdmAlarmInfoDto alarmInfoDto) throws RestException {
        List<MdmAlarmDeviceVo> list=alarmService.queryAlarmInfoExport(alarmInfoDto);
        ExcelUtil.exportExcel(list,"告警详情","sheet",MdmAlarmDeviceVo.class,UUID.randomUUID().toString()+".xls",response);
    }

}
